Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./year2.RDS")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 400)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 11.44648 11.49494 11.54263 11.58956 11.63576 11.68126 11.72607 11.77022
## [9] 11.81373 11.85663 11.89893 11.94066 11.98184 12.02250 12.06265 12.10229
## [17] 12.14137 12.17986 12.21772 12.25493 12.29145 12.32725 12.36229 12.39655
## [25] 12.42999 12.46259 12.49430 12.52550 12.55654 12.58730 12.61768 12.64758
## [33] 12.67688 12.70550 12.73332 12.76024 12.78616 12.81097 12.83457 12.85686
## [41] 12.87772 12.89873 12.92105 12.94404 12.96704 12.98938 13.01040 13.02945
## [49] 13.04586 13.05897 13.06999 13.08044 13.09021 13.09917 13.10720 13.11416
## [57] 13.11993 13.12439 13.12740 13.12885 13.12859 13.12652 13.12249 13.11639
## [65] 13.10652 13.09177 13.07294 13.05079 13.02611 12.99967 12.97226 12.94465
## [73] 12.91763 12.89196 12.86843 12.84782 12.82660 12.80119 12.77228 12.74056
## [81] 12.70672 12.67144 12.63542 12.59935 12.56391 12.52980 12.49770 12.46831
## [89] 12.44231 12.42039 12.40007 12.37871 12.35677 12.33474 12.31307 12.29224
## [97] 12.27272 12.25497 12.23947 12.22583 12.21331 12.20187 12.19143 12.18197
## [105] 12.17341 12.16570 12.15879 12.15263 12.14716 12.14233 12.13808 12.13437
## [113] 12.13112 12.12970 12.13115 12.13499 12.14073 12.14788 12.15596 12.16448
## [121] 12.17296 12.18091 12.18784 12.19326 12.19670 12.19933 12.20260 12.20640
## [129] 12.21063 12.21521 12.22003 12.22499 12.23001 12.23496 12.23977 12.24434
## [137] 12.24856 12.25233 12.25557 12.25734 12.25710 12.25526 12.25226 12.24852
## [145] 12.24448 12.24056 12.23719 12.23480 12.23382 12.23468 12.23781 12.24364
## [153] 12.25259 12.26407 12.27708 12.29143 12.30694 12.32343 12.34070 12.35858
## [161] 12.37688 12.39542 12.41401 12.43246 12.45060 12.46825 12.48520 12.50508
## [169] 12.53091 12.56164 12.59624 12.63364 12.67282 12.71272 12.75230 12.79051
## [177] 12.82630 12.85864 12.88647 12.90876 12.92445 12.93817 12.95479 12.97360
## [185] 12.99387 13.01490 13.03598 13.05639 13.07543 13.09238 13.10653 13.11717
## [193] 13.12358 13.12507 13.12090 13.11066 13.09508 13.07517 13.05199 13.02654
## [201] 12.99987 12.97301 12.94699 12.92283 12.89599 12.86193 12.82168 12.77629
## [209] 12.72680 12.67425 12.61967 12.56412 12.50862 12.45422 12.40196 12.35289
## [217] 12.30803 12.26844 12.23014 12.18892 12.14550 12.10055 12.05479 12.00890
## [225] 11.96358 11.91953 11.87745 11.83803 11.80197 11.76996 11.73967 11.70852
## [233] 11.67686 11.64504 11.61339 11.58227 11.55201 11.52296 11.49547 11.46988
## [241] 11.44654 11.42578 11.40796 11.39342 11.38069 11.36831 11.35661 11.34594
## [249] 11.33664 11.32904 11.32348 11.32031 11.31986 11.32370 11.33269 11.34614
## [257] 11.36334 11.38361 11.40623 11.43052 11.45576 11.48127 11.50634 11.53027
## [265] 11.55237 11.57193 11.58825 11.60509 11.62612 11.65053 11.67751 11.70626
## [273] 11.73595 11.76579 11.79496 11.82266 11.84807 11.87039 11.88881 11.90472
## [281] 11.92010 11.93504 11.94959 11.96384 11.97785 11.99170 12.00546 12.01921
## [289] 12.03302 12.04696 12.06110 12.07551 12.09028 12.10420 12.11632 12.12713
## [297] 12.13709 12.14665 12.15630 12.16650 12.17772 12.19041 12.20407 12.21782
## [305] 12.23168 12.24561 12.25960 12.27366 12.28775 12.30187 12.31601 12.33015
## [313] 12.34428 12.35839 12.37246 12.38648 12.40044 12.41432 12.42812 12.44181
## [321] 12.45539 12.47075 12.48918 12.50976 12.53159 12.55375 12.57534 12.59544
## [329] 12.61313 12.62751 12.63940 12.65027 12.66023 12.66936 12.67776 12.68553
## [337] 12.69276 12.69953 12.70596 12.71212 12.71811 12.72403 12.72997 12.73603
## [345] 12.74158 12.74607 12.74965 12.75250 12.75477 12.75663 12.75826 12.75981
## [353] 12.76144 12.76333 12.76564 12.76854 12.77209 12.77619 12.78066 12.78535
## [361] 12.79011 12.79478 12.79922 12.80326 12.80674 12.80953 12.81145 12.81236
## [369] 12.81209 12.81051 12.80812 12.80553 12.80267 12.79946 12.79586 12.79178
## [377] 12.78717 12.78197 12.77610 12.76969 12.76288 12.75567 12.74804 12.73998
## [385] 12.73147 12.72249 12.71303 12.70309 12.69263 12.68166 12.67015 12.65803
## [393] 12.64527 12.63190 12.61794 12.60343 12.58839 12.57287 12.55688 12.54046
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 400)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 10.78436 10.86879 10.95170 11.03305 11.11283 11.19102 11.26759 11.34254
## [9] 11.41583 11.48745 11.55738 11.62559 11.69207 11.75681 11.81977 11.88096
## [17] 11.94042 11.99819 12.05429 12.10876 12.16161 12.21289 12.26262 12.31083
## [25] 12.35756 12.40282 12.44666 12.48876 12.52885 12.56702 12.60335 12.63791
## [33] 12.67080 12.70209 12.73187 12.76022 12.78721 12.81294 12.83748 12.86092
## [41] 12.88334 12.90320 12.91935 12.93248 12.94324 12.95232 12.96038 12.96809
## [49] 12.97614 12.98518 12.99350 12.99915 13.00241 13.00361 13.00304 13.00101
## [57] 12.99782 12.99378 12.98920 12.98437 12.97961 12.97522 12.97150 12.96877
## [65] 12.96653 12.96405 12.96126 12.95807 12.95440 12.95017 12.94530 12.93972
## [73] 12.93334 12.92608 12.91786 12.90860 12.89841 12.88750 12.87587 12.86358
## [81] 12.85064 12.83709 12.82297 12.80830 12.79311 12.77744 12.76131 12.74476
## [89] 12.72783 12.71053 12.69288 12.67483 12.65632 12.63730 12.61773 12.59756
## [97] 12.57674 12.55522 12.53295 12.50713 12.47566 12.43957 12.39986 12.35753
## [105] 12.31362 12.26911 12.22504 12.18240 12.14222 12.10549 12.07324 12.04648
## [113] 12.02621 12.00889 11.99061 11.97179 11.95290 11.93435 11.91661 11.90010
## [121] 11.88528 11.87258 11.86245 11.85533 11.85166 11.85134 11.85380 11.85875
## [129] 11.86591 11.87500 11.88575 11.89786 11.91105 11.92506 11.93958 11.95435
## [137] 11.96908 11.98348 11.99729 12.01297 12.03284 12.05631 12.08282 12.11180
## [145] 12.14266 12.17485 12.20779 12.24091 12.27364 12.30541 12.33565 12.36378
## [153] 12.38923 12.41466 12.44278 12.47310 12.50515 12.53845 12.57251 12.60686
## [161] 12.64103 12.67452 12.70687 12.73759 12.76620 12.79222 12.81519 12.83676
## [169] 12.85881 12.88116 12.90360 12.92596 12.94805 12.96967 12.99064 13.01077
## [177] 13.02987 13.04775 13.06423 13.07911 13.09221 13.10603 13.12277 13.14174
## [185] 13.16227 13.18370 13.20536 13.22657 13.24665 13.26495 13.28079 13.29349
## [193] 13.30239 13.30681 13.30608 13.30171 13.29565 13.28791 13.27850 13.26741
## [201] 13.25465 13.24023 13.22415 13.20642 13.18484 13.15770 13.12573 13.08968
## [209] 13.05029 13.00828 12.96440 12.91939 12.87399 12.82892 12.78494 12.74277
## [217] 12.70316 12.66684 12.63035 12.59019 12.54709 12.50177 12.45495 12.40735
## [225] 12.35968 12.31268 12.26705 12.22353 12.18282 12.14565 12.10881 12.06899
## [233] 12.02682 11.98291 11.93788 11.89234 11.84691 11.80220 11.75883 11.71741
## [241] 11.67856 11.64289 11.61102 11.58357 11.55798 11.53170 11.50537 11.47960
## [249] 11.45502 11.43227 11.41196 11.39473 11.38119 11.37150 11.36515 11.36182
## [257] 11.36117 11.36290 11.36666 11.37214 11.37901 11.38693 11.39559 11.40466
## [265] 11.41381 11.42272 11.43105 11.44080 11.45383 11.46963 11.48770 11.50754
## [273] 11.52864 11.55049 11.57260 11.59446 11.61556 11.63541 11.65349 11.67199
## [281] 11.69318 11.71668 11.74212 11.76910 11.79727 11.82623 11.85561 11.88503
## [289] 11.91411 11.94247 11.96974 11.99554 12.01949 12.04351 12.06938 12.09653
## [297] 12.12437 12.15231 12.17979 12.20620 12.23097 12.25352 12.27460 12.29536
## [305] 12.31579 12.33589 12.35567 12.37512 12.39424 12.41303 12.43149 12.44962
## [313] 12.46741 12.48487 12.50199 12.51877 12.53522 12.55133 12.56710 12.58253
## [321] 12.59761 12.61289 12.62874 12.64491 12.66117 12.67728 12.69300 12.70810
## [329] 12.72233 12.73545 12.74717 12.75748 12.76655 12.77457 12.78172 12.78817
## [337] 12.79410 12.79971 12.80516 12.81064 12.81633 12.82241 12.82906 12.83646
## [345] 12.84406 12.85122 12.85802 12.86452 12.87081 12.87693 12.88298 12.88901
## [353] 12.89510 12.90131 12.90772 12.91440 12.92142 12.92874 12.93630 12.94401
## [361] 12.95180 12.95957 12.96725 12.97477 12.98203 12.98897 12.99550 13.00153
## [369] 13.00700 13.01182 13.01628 13.02069 13.02501 13.02920 13.03322 13.03703
## [377] 13.04058 13.04383 13.04675 13.04941 13.05190 13.05421 13.05635 13.05828
## [385] 13.06002 13.06154 13.06285 13.06392 13.06475 13.06534 13.06567 13.06569
## [393] 13.06540 13.06481 13.06393 13.06279 13.06140 13.05979 13.05797 13.05596
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 400)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 10.85937 10.91925 10.97813 11.03598 11.09282 11.14862 11.20338 11.25710
## [9] 11.30977 11.36139 11.41193 11.46141 11.50980 11.55712 11.60334 11.64851
## [17] 11.69268 11.73584 11.77799 11.81912 11.85922 11.89830 11.93633 11.97333
## [25] 12.00927 12.04416 12.07799 12.11060 12.14187 12.17185 12.20060 12.22817
## [33] 12.25463 12.28001 12.30439 12.32780 12.35031 12.37198 12.39285 12.41298
## [41] 12.43243 12.45124 12.46938 12.48680 12.50344 12.51924 12.53414 12.54809
## [49] 12.56102 12.57287 12.58385 12.59416 12.60377 12.61264 12.62072 12.62799
## [57] 12.63440 12.63991 12.64449 12.64810 12.65070 12.65225 12.65272 12.65206
## [65] 12.64970 12.64526 12.63897 12.63109 12.62186 12.61151 12.60031 12.58849
## [73] 12.57629 12.56396 12.55174 12.53989 12.52629 12.50906 12.48877 12.46599
## [81] 12.44128 12.41522 12.38837 12.36130 12.33459 12.30879 12.28447 12.26222
## [89] 12.24258 12.22614 12.21068 12.19392 12.17633 12.15836 12.14048 12.12313
## [97] 12.10679 12.09192 12.07896 12.06757 12.05703 12.04725 12.03814 12.02962
## [105] 12.02160 12.01399 12.00672 11.99969 11.99282 11.98603 11.97922 11.97232
## [113] 11.96523 11.95878 11.95369 11.94974 11.94672 11.94440 11.94255 11.94097
## [121] 11.93942 11.93768 11.93554 11.93276 11.92914 11.92494 11.92060 11.91618
## [129] 11.91172 11.90729 11.90292 11.89866 11.89457 11.89070 11.88709 11.88380
## [137] 11.88087 11.87836 11.87631 11.87271 11.86595 11.85669 11.84558 11.83328
## [145] 11.82043 11.80770 11.79574 11.78521 11.77676 11.77104 11.76871 11.77042
## [153] 11.77683 11.78720 11.80014 11.81537 11.83259 11.85150 11.87183 11.89327
## [161] 11.91553 11.93832 11.96135 11.98432 12.00695 12.02895 12.05001 12.07383
## [169] 12.10365 12.13848 12.17733 12.21921 12.26313 12.30811 12.35315 12.39728
## [177] 12.43950 12.47883 12.51427 12.54484 12.56955 12.59364 12.62237 12.65475
## [185] 12.68982 12.72660 12.76412 12.80140 12.83747 12.87136 12.90209 12.92869
## [193] 12.95019 12.96562 12.97399 12.97688 12.97672 12.97384 12.96855 12.96116
## [201] 12.95201 12.94139 12.92964 12.91708 12.90039 12.87671 12.84701 12.81227
## [209] 12.77344 12.73151 12.68745 12.64222 12.59680 12.55215 12.50925 12.46906
## [217] 12.43257 12.40074 12.36837 12.33042 12.28809 12.24257 12.19505 12.14673
## [225] 12.09880 12.05246 12.00890 11.96931 11.93490 11.90685 11.88251 11.85850
## [233] 11.83493 11.81189 11.78949 11.76782 11.74698 11.72708 11.70821 11.69048
## [241] 11.67399 11.65883 11.64510 11.63291 11.62463 11.62171 11.62284 11.62674
## [249] 11.63212 11.63768 11.64214 11.64420 11.64257 11.63903 11.63620 11.63409
## [257] 11.63267 11.63193 11.63186 11.63245 11.63369 11.63556 11.63805 11.64116
## [265] 11.64486 11.64915 11.65401 11.65947 11.66552 11.67214 11.67928 11.68690
## [273] 11.69495 11.70339 11.71220 11.72131 11.73070 11.74032 11.75013 11.76055
## [281] 11.77196 11.78421 11.79717 11.81071 11.82469 11.83899 11.85345 11.86797
## [289] 11.88239 11.89658 11.91041 11.92375 11.93647 11.95020 11.96627 11.98405
## [297] 12.00294 12.02230 12.04152 12.05998 12.07706 12.09213 12.10631 12.12104
## [305] 12.13622 12.15175 12.16751 12.18342 12.19937 12.21524 12.23095 12.24638
## [313] 12.26144 12.27602 12.29001 12.30332 12.31585 12.32747 12.33811 12.34764
## [321] 12.35598 12.36288 12.36839 12.37282 12.37646 12.37961 12.38258 12.38566
## [329] 12.38916 12.39337 12.39823 12.40342 12.40883 12.41439 12.42000 12.42558
## [337] 12.43105 12.43630 12.44126 12.44583 12.44993 12.45346 12.45635 12.45850
## [345] 12.45930 12.45838 12.45605 12.45262 12.44838 12.44363 12.43868 12.43384
## [353] 12.42939 12.42565 12.42291 12.42148 12.42054 12.41913 12.41734 12.41529
## [361] 12.41305 12.41074 12.40844 12.40625 12.40428 12.40261 12.40135 12.40060
## [369] 12.40044 12.40098 12.40181 12.40250 12.40315 12.40382 12.40462 12.40561
## [377] 12.40688 12.40851 12.41059 12.41297 12.41547 12.41811 12.42090 12.42385
## [385] 12.42697 12.43029 12.43382 12.43756 12.44154 12.44577 12.45025 12.45505
## [393] 12.46019 12.46562 12.47134 12.47732 12.48352 12.48992 12.49650 12.50322
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")
keeping in case
#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")